home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / programr / what.zip / DLLDEMO.C_ / DLLDEMO.bin
Text File  |  1992-07-16  |  10KB  |  371 lines

  1. /*****************************************************************************
  2. *
  3. *    dlldemo.c    
  4. *
  5. ******************************************************************************
  6. *    (C) Copyright Microsoft Corp. 1991.  All rights reserved.             *
  7. *                                             *
  8. *    You have a royalty-free right to use, modify, reproduce and         *
  9. *    distribute the Sample Files (and/or any modified version) in          *
  10. *    any way you find useful, provided that you agree that              *
  11. *    Microsoft has no warranty obligations or liability for any              *
  12. *    Sample Application Files which are modified.                            *
  13. ******************************************************************************
  14. *
  15. *  Test Notes: 
  16. *    CodeView.
  17. *      1) Make a progman item with the following instruction:
  18. *          "cvw /l dlldemo.dll c:\windows\winhelp.exe dlldemo.hlp"
  19. *            (assuming you installed windows in the "c:\windows" directory).
  20. *      3) during startup, it will say, debug information not found.  Ignore
  21. *            this message- it is because it can't find debug info for
  22. *            winhelp internal routines which you don't need.
  23. *         after startup, you will be in cvw window.  Pulldown File:Load Module.
  24. *      4) select libmain.c
  25. *      5) page down and set a breakpoint in LibMain() [click on a line]
  26. *      6) press F5 or click on the run button to Go.  Winhelp will load,
  27. *            and then break into Codeview at the LibMain() function.
  28. *
  29. *****************************************************************************/
  30.  
  31.  
  32. #include <windows.h>
  33. #include "dll.h"
  34. #include "dlldemo.h"
  35.  
  36. #define    PUBLIC    extern        /* Public label.        */
  37. #define    PRIVATE    static        /* Private label.        */
  38. #define    DEFAULT            /* Default function.        */
  39. #define    EXPORT    FAR _loadds    /* Export function.        */
  40.  
  41.  
  42. char   szDTClassName[] = "DisplayTopic";     /* Extended Window class name */
  43. char   szDTTitle[] = "Topic Information";    /* Extended Window Title */
  44. BOOL   fDisplayDT = FALSE;
  45. HWND   hwndDT = NULL;
  46. LONG   lOffsetDT;
  47.  
  48. HANDLE ghModule;
  49.  
  50. long EXPORT PASCAL DisplayTopicProc (HWND hwnd,
  51.                  UINT wMsg,
  52.                  WPARAM wParam,
  53.                  LPARAM lParam);
  54.  
  55.  
  56. LPFN_HFSOPENSZ         lpfn_HfsOpenSz;
  57. LPFN_RCCLOSEHFS        lpfn_RcCloseHfs;
  58. LPFN_RCLLINFOFROMHF    lpfn_RcLLInfoFromHf;
  59. LPFN_FACCESSHFS        lpfn_FAccessHfs;
  60. LPFN_HFOPENHFS        lpfn_HfOpenHfs;
  61. LPFN_LCBREADHF        lpfn_LcbReadHf;
  62. LPFN_RCCLOSEHF        lpfn_RcCloseHf;
  63.  
  64. /***************************************************************************
  65.  *
  66.  -  Name    HelloWorld
  67.  -
  68.  *  Purpose    The most trivial of DLL functions.
  69.  *
  70.  ***************************************************************************/
  71. PUBLIC    BOOL PASCAL EXPORT HelloWorld( LONG hwndContext, LPSTR lszHlpFile )
  72. {
  73.     MessageBeep(0);
  74.     return TRUE;
  75. }
  76.  
  77.  
  78. /***************************************************************************
  79.  *
  80.  -  Name    FInitExtendedWindow
  81.  -
  82.  *  Purpose    Called by LibMain during initialization of DLL.
  83.  *
  84.  *  Arguments    hModule     Module handle for the library
  85.  *
  86.  *  Returns     
  87.  *
  88.  *  +++
  89.  *
  90.  *  Notes
  91.  *
  92.  ***************************************************************************/
  93.  
  94. BOOL FInitExtendedWindow( HANDLE hModule )
  95.   {
  96.   WNDCLASS wc;
  97.  
  98.   ghModule = hModule;
  99.  
  100.   wc.lpszClassName  = szDTClassName;
  101.   wc.style          = CS_GLOBALCLASS;
  102.   wc.hCursor        = LoadCursor(NULL, IDC_ARROW);
  103.   wc.hIcon          = NULL;
  104.   wc.lpszMenuName   = NULL;
  105.   wc.hbrBackground  = COLOR_WINDOW + 1;
  106.   wc.hInstance      = hModule;
  107.   wc.lpfnWndProc    = DisplayTopicProc;
  108.   wc.cbClsExtra     = 0;
  109.   wc.cbWndExtra     = 0;
  110.  
  111.   if (!RegisterClass (&wc))
  112.     return FALSE; /* Initialization  failed */
  113.  
  114.   return TRUE;  /* if we got here everything is ok! */
  115.   }
  116.  
  117.  
  118. /***************************************************************************
  119.  *
  120.  -  Name    DisplayTopicInfo
  121.  -
  122.  *  Purpose    This DLL function shows the Display Topic extended 
  123.  *              window.
  124.  *
  125.  *  Arguments    handle to the main Help window
  126.  *
  127.  ***************************************************************************/
  128. PUBLIC    void PASCAL EXPORT DisplayTopicInfo(
  129.     LONG    hwndParent)
  130. {
  131.     if (hwndDT == NULL)
  132.         {
  133.         hwndDT = CreateWindow( szDTClassName,
  134.               szDTTitle,
  135.               WS_CAPTION | WS_POPUPWINDOW,
  136.               100, 100, 400, 100,
  137.               (WORD)hwndParent, NULL, 
  138.               ghModule, 
  139.               0L );
  140.         }
  141.         fDisplayDT = TRUE;
  142.     ShowWindow( hwndDT, SW_SHOW );
  143. }
  144.  
  145.  
  146. /***************************************************************************
  147.  *
  148.  -  Name    DisplayTopicProc
  149.  -
  150.  *  Purpose    Displays topic information
  151.  *
  152.  *  Arguments    hwnd    This window's handle
  153.  *        wMsg    The current message
  154.  *        wParam    Message dependent
  155.  *        lParam    Message dependent
  156.  *
  157.  *  Returns    Message dependent
  158.  *
  159.  *  +++
  160.  *
  161.  *  Notes
  162.  *    This only displays the current topic offset number.  Ordinarily,
  163.  *    you would have to look this number up in a table to give 
  164.  *    more meaningful information.
  165.  *
  166.  ***************************************************************************/
  167.  
  168. long EXPORT PASCAL DisplayTopicProc( HWND hwnd,
  169.                  UINT wMsg,
  170.                  WPARAM wParam,
  171.                  LPARAM lParam )
  172.   {
  173.   PAINTSTRUCT ps;
  174.   TEXTMETRIC  tm;
  175.   char rgchBuf[30];
  176.  
  177.   switch (wMsg)
  178.     {
  179.     case WM_CREATE:
  180.  
  181.       return 0L;
  182.  
  183.     case WM_PAINT:
  184.       BeginPaint( hwnd, &ps );
  185.       GetTextMetrics( ps.hdc, &tm );
  186.       SetTextColor( ps.hdc, GetSysColor( COLOR_WINDOWTEXT ) );
  187.       SetBkColor( ps.hdc, GetSysColor( COLOR_WINDOW ) );
  188.       wsprintf( rgchBuf, "Topic Offset: %ld", lOffsetDT );
  189.       TextOut( ps.hdc, tm.tmMaxCharWidth/2,
  190.          (tm.tmHeight + tm.tmExternalLeading)/2,
  191.          rgchBuf, lstrlen(rgchBuf) );
  192.       EndPaint( hwnd, &ps );
  193.       return 0L;
  194.  
  195.     case WM_CLOSE:
  196.       fDisplayDT = FALSE;
  197.       hwndDT = NULL;
  198.       break;
  199.     }
  200.  
  201.     return DefWindowProc (hwnd, wMsg, wParam, lParam);
  202.   }
  203.  
  204.  
  205.  
  206. /***************************************************************************
  207.  *
  208.  -  Name    ExportBag
  209.  -
  210.  *  Purpose    Copies a Baggage file to a DOS file.
  211.  *
  212.  *  Arguments    lszHLPname:    Name of Help file
  213.  *              lszBagFName:   Name of internal Baggage file
  214.  *              lszExportName: Name of DOS file to copy to
  215.  *              qError:        Help error structure
  216.  *
  217.  *  Returns     TRUE if successful, FALSE otherwise.
  218.  *
  219.  ***************************************************************************/
  220.  
  221. #define    wCOPY_SIZE    (WORD)32768
  222.  
  223. PUBLIC    BOOL PASCAL EXPORT ExportBag(
  224. LPSTR      lszHLPname,
  225. LPSTR    lszBagFName,
  226. LPSTR    lszExportName,
  227. QME     qError)
  228. {
  229.     HANDLE    hfsHlp;        // handle to .HLP file
  230.     HANDLE    hfBag;        // file handle to bag file
  231.     HANDLE    hFile;        // handle to output file
  232.     BOOL    fClean = TRUE;    // if set, Delete file in Error state Machine.
  233.     DWORD    dwBytesRead;    // input bytes read
  234.     HANDLE    hMem;        // handle to copy buffer
  235.     LPBYTE    lpMem;        // ptr to copy buffer
  236.     OFSTRUCT ofs;
  237.  
  238.     hfsHlp = hfBag = hMem = NULL;
  239.     hFile = (HANDLE)-1;
  240.  
  241.     if ((hfsHlp = (*lpfn_HfsOpenSz)(lszHLPname, fFSOpenReadOnly)) == NULL)
  242.         {
  243.         qError->fwFlags = fwMERR_ABORT;
  244.         qError->wError = wMERR_PARAM;
  245.         goto ExitBag;
  246.         }
  247.  
  248.     if (((*lpfn_FAccessHfs)(hfsHlp, lszBagFName, NULL)) == FALSE)
  249.         {
  250.         qError->fwFlags = fwMERR_RETRY;
  251.         qError->wError = wMERR_MESSAGE;
  252.         lstrcpy(qError->rgchError, "Could not open baggage file `");
  253.         lstrcat(qError->rgchError, lszBagFName);
  254.         lstrcat(qError->rgchError, "'.");
  255.         goto ExitBag;
  256.         }
  257.  
  258.     if ((hfBag = (*lpfn_HfOpenHfs)(hfsHlp, lszBagFName, fFSOpenReadOnly))
  259.         == NULL)
  260.         {
  261.         qError->fwFlags = fwMERR_ABORT;
  262.         qError->wError = wMERR_ERROR;
  263.         goto ExitBag;
  264.         }
  265.  
  266.     if ((hMem = GlobalAlloc(GMEM_MOVEABLE,
  267.         (DWORD)wCOPY_SIZE)) == NULL)
  268.         {
  269.         qError->fwFlags = fwMERR_ABORT;
  270.         qError->wError = wMERR_MEMORY;
  271.         goto ExitBag;
  272.         }
  273.  
  274.     lpMem = GlobalLock(hMem);
  275.     OpenFile(lszExportName, &ofs, OF_DELETE);
  276.     if ((hFile = _lcreat(lszExportName,0)) == -1)
  277.         {
  278.         qError->fwFlags = fwMERR_ABORT;
  279.         qError->wError = wMERR_ERROR;
  280.         goto ExitBag;
  281.         }
  282.     
  283.     do  {
  284.         if ((dwBytesRead =
  285.         (*lpfn_LcbReadHf)(hfBag, lpMem, wCOPY_SIZE)) == -1L)
  286.         {
  287.             qError->fwFlags = fwMERR_ABORT;
  288.             qError->wError = wMERR_ERROR;
  289.         goto ExitBag;
  290.         }
  291.         if (_lwrite(hFile, lpMem,(WORD) dwBytesRead) != (WORD) dwBytesRead)
  292.         {
  293.             qError->fwFlags = fwMERR_ABORT | fwMERR_RETRY;
  294.             qError->wError = wMERR_MESSAGE;
  295.         lstrcpy(qError->rgchError, "Out of disk space.");
  296.         goto ExitBag;
  297.         }
  298.         } while (dwBytesRead == wCOPY_SIZE);
  299.  
  300. ExitBag:
  301.         if (hfBag != NULL)
  302.         (*lpfn_RcCloseHf)(hfBag);
  303.     if (hfsHlp != NULL)
  304.         (*lpfn_RcCloseHfs)(hfsHlp);
  305.     if (hMem != NULL)
  306.         {
  307.         GlobalUnlock(hMem);
  308.         GlobalFree(hMem);
  309.         }
  310.     if (hFile != -1)
  311.         _lclose(hFile);
  312.     if (qError->wError != wMERR_NONE && fClean)
  313.         OpenFile(lszExportName, &ofs, OF_DELETE);
  314.  
  315.     return (qError->wError == wMERR_NONE);
  316. }
  317.  
  318.  
  319.  
  320. PUBLIC    BOOL PASCAL GetCallBacks(
  321. VPTR    VPtr,
  322. LONG    lVersion)
  323. {
  324.  
  325.     // hfs level:
  326.      lpfn_HfsOpenSz         = (LPFN_HFSOPENSZ) VPtr[HE_HfsOpenSz];
  327.     lpfn_RcCloseHfs     = (LPFN_RCCLOSEHFS) VPtr[HE_RcCloseHfs];
  328.  
  329.     // bag level routines
  330.     lpfn_FAccessHfs        = (LPFN_FACCESSHFS) VPtr[HE_FAccessHfs];
  331.      lpfn_HfOpenHfs      = (LPFN_HFOPENHFS) VPtr[HE_HfOpenHfs];
  332.     lpfn_LcbReadHf      = (LPFN_LCBREADHF) VPtr[HE_LcbReadHf];
  333.     lpfn_RcCloseHf        = (LPFN_RCCLOSEHF) VPtr[HE_RcCloseHf];
  334.     lpfn_RcLLInfoFromHf      = (LPFN_RCLLINFOFROMHF) VPtr[HE_RcLLInfoFromHf];
  335.  
  336.     return TRUE;
  337. }
  338.  
  339.  
  340. PUBLIC    LONG PASCAL EXPORT LDLLHandler(
  341.     WORD    wMsg,
  342.     LONG    lParam1,
  343.     LONG    lParam2)
  344. {
  345.     switch(wMsg)
  346.         {
  347.     case DW_WHATMSG:
  348.         return DC_INITTERM | DC_CALLBACKS | DC_MINMAX | DC_JUMP;
  349.     case DW_CALLBACKS:
  350.         GetCallBacks((VPTR)lParam1,lParam2);
  351.         return TRUE;
  352.     case DW_MINMAX:
  353.         if ( fDisplayDT )
  354.             ShowWindow( hwndDT, (lParam1 == 1L ? SW_HIDE : SW_SHOW) );
  355.         return TRUE;
  356.     case DW_ENDJUMP:
  357.         lOffsetDT = lParam1;
  358.         InvalidateRect( hwndDT, NULL, TRUE );
  359.         return TRUE;
  360.     case DW_CHGFILE:
  361.     case DW_TERM:
  362.         ShowWindow( hwndDT, SW_HIDE );
  363.         fDisplayDT = FALSE;
  364.         return TRUE;
  365.     case DW_INIT:
  366.         return TRUE;
  367.         }
  368.     return FALSE;
  369. }
  370.  
  371.